package cn.byapps.master.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.byapps.master.entity.TaskRecord;
import cn.byapps.master.service.TaskService;
import cn.byapps.master.vo.Result;

@Controller
@RequestMapping("task")
@CrossOrigin
//@Slf4j
public class TaskAPIController {
	@Value("${upload.url}")
	private String tempURL;
	@Value("${upload.path}")
	private String path;

	@Autowired
	private TaskService taskService;

	@PostMapping("add")
	@ResponseBody
	public Result add(TaskRecord item, MultipartFile file, HttpServletRequest req)
			throws Exception, IOException {
		File f = new File(path);
		if (!f.exists())
			f.mkdirs();
		file.transferTo(new File(path, file.getOriginalFilename()));
		item.setImgPath(tempURL + file.getOriginalFilename());
		taskService.save(item);
		
		return new Result(0,"成功","","");
	}

}
